<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Python,Workflowy,Teambition," />





  <link rel="alternate" href="/atom.xml" title="Kingname" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="Teambition是一个跨平台的团队协作和项目管理工具，相当于国外的Trello。使用Teambition可以像使用白板与便签纸一样来管理项目进度，如下图所示。

Teambition虽然便于管理项目，但是如果直接在Teambition上面创建一个项目对应的任务，却容易陷入面对茫茫白板，不知道如何拆分任务的尴尬境地。如下图所示。

面对这个空荡荡的窗口，应该添加哪些任务进去？直接用脑子现想，恐怕">
<meta property="og:type" content="article">
<meta property="og:title" content="TeamFlowy——结合Teambition与Workflowy">
<meta property="og:url" content="https://kingname.info/2017/10/03/teamflowy/index.html">
<meta property="og:site_name" content="Kingname">
<meta property="og:description" content="Teambition是一个跨平台的团队协作和项目管理工具，相当于国外的Trello。使用Teambition可以像使用白板与便签纸一样来管理项目进度，如下图所示。

Teambition虽然便于管理项目，但是如果直接在Teambition上面创建一个项目对应的任务，却容易陷入面对茫茫白板，不知道如何拆分任务的尴尬境地。如下图所示。

面对这个空荡荡的窗口，应该添加哪些任务进去？直接用脑子现想，恐怕">
<meta property="og:image" content="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-03-23-45-13.png">
<meta property="og:image" content="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-03-23-47-38.png">
<meta property="og:image" content="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-03-23-53-06.png">
<meta property="og:image" content="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-10-28-13.png">
<meta property="og:image" content="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-10-47-51.png">
<meta property="og:image" content="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-10-55-40.png">
<meta property="og:image" content="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-16-09-03.png">
<meta property="og:image" content="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-16-14-38.png">
<meta property="og:image" content="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-16-18-20.png">
<meta property="og:image" content="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-16-20-49.png">
<meta property="og:image" content="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-16-31-25.png">
<meta property="og:image" content="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-16-45-51.png">
<meta property="og:image" content="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-16-55-34.png">
<meta property="og:image" content="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-17-01-11.png">
<meta property="og:updated_time" content="2017-10-27T15:27:17.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="TeamFlowy——结合Teambition与Workflowy">
<meta name="twitter:description" content="Teambition是一个跨平台的团队协作和项目管理工具，相当于国外的Trello。使用Teambition可以像使用白板与便签纸一样来管理项目进度，如下图所示。

Teambition虽然便于管理项目，但是如果直接在Teambition上面创建一个项目对应的任务，却容易陷入面对茫茫白板，不知道如何拆分任务的尴尬境地。如下图所示。

面对这个空荡荡的窗口，应该添加哪些任务进去？直接用脑子现想，恐怕">
<meta name="twitter:image" content="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-03-23-45-13.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://kingname.info/2017/10/03/teamflowy/"/>





  <title> TeamFlowy——结合Teambition与Workflowy | Kingname </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">Kingname</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">给时光以生命。</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tweet">
          <a href="/tweet" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-twitter"></i> <br />
            
            时间
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="https://kingname.info/2017/10/03/teamflowy/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="Kingname">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="/images/avatar.png">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="Kingname">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="Kingname" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                TeamFlowy——结合Teambition与Workflowy
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-10-03T23:29:05+08:00">
                2017-10-03
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Craft/" itemprop="url" rel="index">
                    <span itemprop="name">Craft</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <a href="/2017/10/03/teamflowy/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2017/10/03/teamflowy/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>Teambition是一个跨平台的团队协作和项目管理工具，相当于国外的Trello。使用Teambition可以像使用白板与便签纸一样来管理项目进度，如下图所示。</p>
<p><img src="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-03-23-45-13.png" alt=""></p>
<p>Teambition虽然便于管理项目，但是如果直接在Teambition上面创建一个项目对应的任务，却容易陷入面对茫茫白板，不知道如何拆分任务的尴尬境地。如下图所示。</p>
<p><img src="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-03-23-47-38.png" alt=""></p>
<p>面对这个空荡荡的窗口，应该添加哪些任务进去？直接用脑子现想，恐怕容易出现顾此失彼或者干脆漏掉了任务的情况。</p>
<p>当我要开始一个项目的时候，我一般不会直接打开Teambition就写任务，而是使用一个大纲工具——Workflowy来梳理思路，切分任务。等任务已经切分好了，在誊写到Teambition中，如下图所示。</p>
<p><img src="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-03-23-53-06.png" alt=""></p>
<p>但这样就出现了一个问题：首先在Workflowy上面把需要做的任务写好。然后再打开Teambition，把这些任务又誊写到Teambition中。为了减少“誊写”这一步重复劳动，于是就有了TeamFlowy这个小工具。它的作用是自动誊写Workflowy中的特定条目到Teambition中。</p>
<a id="more"></a>
<h2 id="功能介绍"><a href="#功能介绍" class="headerlink" title="功能介绍"></a>功能介绍</h2><p>TeamFlowy是一个Python脚本，运行以后，它会登录Workflowy账号，读取上面所有的条目。名为[Teambition]的条目是任务开始的标记，这个条目下面的一级条目会作为任务被添加到Teambition中。如果任务下面还有二级条目，这些二级条目会作为子任务被添加到任务中。由于Teambition是按照项目-Stage-任务-子任务的形式组织一个工程（其中Stage对应了Teambition中工程下面的面板，例如：“待处理”，“进行中”，“完成”。）不会存在子任务的子任务，所以Workflowy中[Teambition]这个条目下面最多出现二级缩进。如下图所示。</p>
<p><img src="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-10-28-13.png" alt=""></p>
<h2 id="实现原理"><a href="#实现原理" class="headerlink" title="实现原理"></a>实现原理</h2><h3 id="Workflowy"><a href="#Workflowy" class="headerlink" title="Workflowy"></a>Workflowy</h3><p>获取Workflowy上面的条目，需要进行三步操作：</p>
<ol>
<li>登录Workflowy</li>
<li>获取所有条目对应的JSON字符串</li>
<li>提取需要添加到Teambition中的条目</li>
</ol>
<h4 id="登录Workflowy"><a href="#登录Workflowy" class="headerlink" title="登录Workflowy"></a>登录Workflowy</h4><p>打开Chrome监控登录Wokrflowy的过程，可以看到登录Workflowy需要访问的接口为：<code>https://workflowy.com/accounts/login/</code>。使用HTTP <code>POST</code>方式发送请求，提交的数据包括<code>username</code>，<code>password</code>和一个不知道用途的<code>next</code>。如下图所示。</p>
<p><img src="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-10-47-51.png" alt=""></p>
<p>使用Python的第三方网络模块<code>requests</code>向这个模块发送<code>POST</code>请求，提交用户名和密码即可实现登录。其代码如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">login_url = <span class="string">'https://workflowy.com/accounts/login/'</span></div><div class="line">session = requests.Session()</div><div class="line">session.post(login_url,</div><div class="line">             data=&#123;<span class="string">'username'</span>: <span class="string">'12345@qq.com'</span>,</div><div class="line">                   <span class="string">'password'</span>: <span class="string">'8888888'</span>,</div><div class="line">                   <span class="string">'next'</span>: <span class="string">''</span>&#125;)</div></pre></td></tr></table></figure>
<h4 id="获取所有条目"><a href="#获取所有条目" class="headerlink" title="获取所有条目"></a>获取所有条目</h4><p>使用<code>requests</code>的<code>session</code>登录Workflowy以后，Cookies会被自动保存到<code>session</code>这个对象里面。于是使用<code>session</code>继续访问Workfowy就可以以登录后的身份查看自己的各个条目。</p>
<p>通过Chrome可以看到获取所有条目的接口为<code>https://workflowy.com/get_initialization_data?client_version=18</code>，接口返回的数据是一个包含所有条目的超大型JSON字符串，如下图所示。</p>
<p><img src="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-10-55-40.png" alt=""></p>
<p>使用Python的json模块可以解析这个JSON字符串为字典，并获取所有条目，代码如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">outline_url = <span class="string">'https://workflowy.com/get_initialization_data?client_version=18'</span></div><div class="line">outlines_json = session.get(outline_url).text</div><div class="line">outlines_dict = json.loads(outlines_json)</div><div class="line">project_list = outlines_dict.get(<span class="string">'projectTreeData'</span>, &#123;&#125;)\</div><div class="line">    .get(<span class="string">'mainProjectTreeInfo'</span>, &#123;&#125;)\</div><div class="line">    .get(<span class="string">'rootProjectChildren'</span>, [])</div></pre></td></tr></table></figure>
<h4 id="提取任务与子任务"><a href="#提取任务与子任务" class="headerlink" title="提取任务与子任务"></a>提取任务与子任务</h4><p>所有的条目层层嵌套在列表-字典结构中，其基本的形态如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&#123;</div><div class="line">    <span class="string">"ch"</span>: [子条目], </div><div class="line">    <span class="string">"lm"</span>: <span class="number">308496</span>, </div><div class="line">    <span class="string">"id"</span>: <span class="string">"957996b9-67ce-51c7-a796-bfbee44e3d3f"</span>, </div><div class="line">    <span class="string">"nm"</span>: <span class="string">"AutoEmo"</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>其中的<code>nm</code>为这个条目的名字。如果一个条目有子条目，那么<code>ch</code>列表中就会有很多个字典，每个字典的都是这个结构，如果一个条目没有子条目，那么就没有<code>ch</code>这个key。这样一层一层嵌套下去：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line"> &#123;</div><div class="line">    <span class="string">"ch"</span>: [</div><div class="line">        &#123;</div><div class="line">            <span class="string">"lm"</span>: <span class="number">558612</span>, </div><div class="line">            <span class="string">"id"</span>: <span class="string">"5117e20b-25ba-ba91-59e1-790c0636f78e"</span>, </div><div class="line">            <span class="string">"nm"</span>: <span class="string">"准备并熟背一段自我介绍，在任何需要自我介绍的场合都有用"</span></div><div class="line">        &#125;, </div><div class="line">        &#123;</div><div class="line">            <span class="string">"lm"</span>: <span class="number">558612</span>, </div><div class="line">            <span class="string">"id"</span>: <span class="string">"4894b23e-6f47-8028-a26a-5fb315fc4e6f"</span>, </div><div class="line">            <span class="string">"nm"</span>: <span class="string">"姓名，来自哪里，什么工作"</span>,</div><div class="line">            <span class="string">"ch"</span>: [</div><div class="line">                &#123;<span class="string">"lm"</span>: <span class="number">5435246</span>, </div><div class="line">                 <span class="string">"id"</span>: <span class="string">"4894b23e-6f47-8028-a26a-5fbadfasdc4e6f"</span>, </div><div class="line">                 <span class="string">"nm"</span>: <span class="string">"工作经验"</span>&#125;</div><div class="line">            ]</div><div class="line">        &#125;</div><div class="line">    ], </div><div class="line">    <span class="string">"lm"</span>: <span class="number">558612</span>, </div><div class="line">    <span class="string">"id"</span>: <span class="string">"ea282a1c-94f3-1a44-c5b3-7907792e9e6e"</span>, </div><div class="line">    <span class="string">"nm"</span>: <span class="string">"自我介绍"</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>由于条目和子条目的结构是一样的，那么就可以使用递归来解析每一个条目。由于需要添加到Teambition的任务，从名为[Teambition]的条目开始，于是可以使用下面这样一个函数来解析：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">task_dict = &#123;&#125;</div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">extract_task</span><span class="params">(sections, task_dict, target_section=False)</span>:</span></div><div class="line">    <span class="keyword">for</span> section <span class="keyword">in</span> sections:</div><div class="line">        name = section[<span class="string">'nm'</span>]</div><div class="line">        <span class="keyword">if</span> target_section:</div><div class="line">            task_dict[name] = [x[<span class="string">'nm'</span>] <span class="keyword">for</span> x <span class="keyword">in</span> section.get(<span class="string">'ch'</span>, [])]</div><div class="line">            <span class="keyword">continue</span></div><div class="line"></div><div class="line">        <span class="keyword">if</span> name == <span class="string">'[Teambition]'</span>:</div><div class="line">            target_section = <span class="keyword">True</span></div><div class="line">        sub_sections = section.get(<span class="string">'ch'</span>, [])</div><div class="line">        extract_task(sub_sections, task_dict, target_section=target_section)</div></pre></td></tr></table></figure>
<p>下图所示为一段需要添加到Teambition中的条目，运行这段函数以后，得到的结果为：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&#123;<span class="string">'登录Workflowy'</span>: [], <span class="string">'获取需要添加到Teambition的内容'</span>: [<span class="string">'获取任务'</span>, <span class="string">'获取子任务'</span>], <span class="string">'调试Teambition API'</span>: [], <span class="string">'添加任务到Teambition'</span>: []&#125;</div></pre></td></tr></table></figure>
<p><img src="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-16-09-03.png" alt=""></p>
<h2 id="Teambition"><a href="#Teambition" class="headerlink" title="Teambition"></a>Teambition</h2><p>将任务添加到Teambition，需要使用Teambition的Python SDK登录Teambition并调用API添加任务。Teambition的Python SDK在使用<code>OAuth2</code>获取<code>access_token</code>的时候有一个坑，需要特别注意。</p>
<h3 id="登录Teambition"><a href="#登录Teambition" class="headerlink" title="登录Teambition"></a>登录Teambition</h3><h4 id="设置Teambition应用"><a href="#设置Teambition应用" class="headerlink" title="设置Teambition应用"></a>设置Teambition应用</h4><p>Teambition 是使用<code>OAuth2</code>来进行权限验证的，所以需要获取<code>access_token</code>。</p>
<p>首先打开Teambition的<a href="https://developer.teambition.com/dashboard" target="_blank" rel="external">开发者中心</a>并单击<code>新建应用</code>，如下图所示。</p>
<p><img src="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-16-14-38.png" alt=""></p>
<p>应用名称可以随便写。创建好应用以后，可以看到应用的信息，需要记录<code>Client ID</code>和<code>Client Secret</code>，如下图所示。</p>
<p><img src="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-16-18-20.png" alt=""></p>
<p>点击左侧的<code>OAuth2</code>配置，填写回调URL，如下图所示。这里的这个URL其实使用任何一个可以访问的网站的域名都可以，这里以我的博客地址为例。</p>
<p><img src="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-16-20-49.png" alt=""></p>
<h4 id="使用Python获取access-token"><a href="#使用Python获取access-token" class="headerlink" title="使用Python获取access_token"></a>使用Python获取access_token</h4><p>首先在Python中安装Teambition的SDK：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">pip install teambition</div></pre></td></tr></table></figure>
<p>接下来，在Python中获取授权URL：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">from</span> teambition <span class="keyword">import</span> Teambition</div><div class="line"></div><div class="line">tb_client_id = <span class="string">'7bfae080-a8dc-11e7-b543-77a936726657'</span></div><div class="line">tb_client_secret = <span class="string">'9830fc8c-81b3-45ed-b3c0-e039ab8f2d8b'</span></div><div class="line">tb = Teambition(tb_client_id,</div><div class="line">                tb_client_secret)</div><div class="line">authorize_url = tb.oauth.get_authorize_url(<span class="string">'https://kingname.info'</span>)</div><div class="line">print(authorize_url)</div></pre></td></tr></table></figure>
<p>代码运行以后，会得到一段形如下面这段URL的授权URL：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">https://account.teambition.com/oauth2/authorize?client_id=7bfae080-a8dc-11e7-b543-77a936726657&amp;redirect_uri=https://kingname.info&amp;state=&amp;lang=zh</div></pre></td></tr></table></figure>
<p>在电脑浏览器中<strong>人工</strong>访问这个URL，会出现下面这样的页面。</p>
<p><img src="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-16-31-25.png" alt=""></p>
<p>单击<code>授权并登录</code>，可以看到浏览器上面的网址变为形如：<code>https://kingname.info/?code=Pn7ebs4sZh3NYOz2FvVJQ4uu</code>，此时，需要记录<code>code=</code>后面的这一串字符串<code>Pn7ebs4sZh3NYOz2FvVJQ4uu</code>。</p>
<p>接下来就是Teambition的SDK的坑点了，根据Teambition官方文档的说法，要获取access_token，只需要如下两段代码：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">code = <span class="string">'Pn7ebs4sZh3NYOz2FvVJQ4uu'</span> <span class="comment">#前面浏览器中的字符串</span></div><div class="line">tb.oauth.fetch_access_token(code)</div><div class="line"><span class="comment"># 上面的代码完成授权，接下来直接使用tb.xxxx就可以操作任务了。</span></div></pre></td></tr></table></figure>
<p>但实际上，上面这一段代码一定会报错。提示<code>grant invaild</code>。要解决这个问题，就必需使用Teambition的HTTP 接口来人工获取access_token。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">code = <span class="string">'Pn7ebs4sZh3NYOz2FvVJQ4uu'</span> <span class="comment">#前面浏览器中的字符串</span></div><div class="line">fetch_result_dict = session.post(<span class="string">'https://account.teambition.com/oauth2/access_token'</span>,</div><div class="line">                  data=&#123;<span class="string">'client_id'</span>: tb_client_id,</div><div class="line">                        <span class="string">'client_secret'</span>: tb_client_secret,</div><div class="line">                        <span class="string">'code'</span>: code,</div><div class="line">                        <span class="string">'grant_type'</span>: <span class="string">'code'</span>&#125;).json()</div><div class="line">tb_access_token = fetch_result_dict.get(<span class="string">'access_token'</span>, <span class="string">''</span>)</div></pre></td></tr></table></figure>
<p>此时得到的access_token是一段非常长的字符串。接下来，重新初始化tb变量：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">tb = Teambition(tb_client_id,</div><div class="line">                tb_client_secret,</div><div class="line">                access_token=tb_access_token)</div></pre></td></tr></table></figure>
<p>初始化以后，使用tb这个变量，就可以对工程和任务进行各种操作了。</p>
<h4 id="Teambition的简单使用"><a href="#Teambition的简单使用" class="headerlink" title="Teambition的简单使用"></a>Teambition的简单使用</h4><p>要在某个工程里面创建任务，就需要知道工程的ID。首先在Teambition中手动创建一个工程，在浏览器中打开工程，URL中可以看到工程的ID，如下图所示。</p>
<p><img src="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-16-45-51.png" alt=""></p>
<p>有了工程ID以后，就可以使用下面的代码创建任务：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">create_task</span><span class="params">(task_name, sub_task_list)</span>:</span></div><div class="line">    tasklist = tb.tasklists.get(project_id=<span class="string">'59d396ee1013d919f3348675'</span>)[<span class="number">0</span>]</div><div class="line">    tasklist_id = tasklist[<span class="string">'_id'</span>]</div><div class="line">    todo_stage_id = tasklist[<span class="string">'stageIds'</span>][<span class="number">0</span>]</div><div class="line">    task_info = tb.tasks.create(task_name, tasklist_id=tasklist_id, stage_id=todo_stage_id)</div><div class="line">    <span class="keyword">if</span> sub_task_list:</div><div class="line">        task_id = task_info[<span class="string">'_id'</span>]</div><div class="line">        <span class="keyword">for</span> sub_task_name <span class="keyword">in</span> sub_task_list:</div><div class="line">            tb.subtasks.create(sub_task_name, task_id=task_id)</div><div class="line">    print(f<span class="string">'task: &#123;task_name&#125; with sub tasks: &#123;sub_task_list&#125; added.'</span>)</div></pre></td></tr></table></figure>
<p>这段代码首先使用<code>tb.tasklists.get()</code>根据工程ID获得任务组的ID和<code>待处理</code>这个面板的ID，接下来调用<code>tb.tasks.create()</code>接口添加任务。从添加任务返回的信息里面拿到任务的ID，再根据任务ID，调用<code>tb.subtasks.create()</code>添加子任务ID。</p>
<h2 id="效果测试"><a href="#效果测试" class="headerlink" title="效果测试"></a>效果测试</h2><p>上面的代码实现了TeamFlowy的基本逻辑。运行TeamFlowy脚本以后，[Teambition]这个条目下面的任务被成功的添加到了Teambition中，如下图所示。</p>
<p><img src="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-16-55-34.png" alt=""></p>
<p>将代码组合起来并进行完善，让代码更容易使用，完整的代码可以查看<a href="https://github.com/kingname/TeamFlowy" target="_blank" rel="external">https://github.com/kingname/TeamFlowy</a>。完整的代码运行效果如下图所示。</p>
<p><img src="http://7sbpmp.com1.z0.glb.clouddn.com/2017-10-04-17-01-11.png" alt=""></p>

      
    </div>

    <div>
      
        
<div id="wechat_subscriber" style="display: block; padding: 10px 0; margin: 20px auto; width: 100%; text-align: center">
    <img id="wechat_subscriber_qcode" src="/images/wechatplatform.jpg" alt="Kingname wechat" style="width: 200px; max-width: 100%;"/>
    <div>第一时间获取最新文章更新，请订阅我的微信公众号：未闻Code</div>
</div>


      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Python/" rel="tag"># Python</a>
          
            <a href="/tags/Workflowy/" rel="tag"># Workflowy</a>
          
            <a href="/tags/Teambition/" rel="tag"># Teambition</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/22/tweet-201709/" rel="next" title="技巧收集-M1709">
                <i class="fa fa-chevron-left"></i> 技巧收集-M1709
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/12/10/use-fsm/" rel="prev" title="使用有限状态机原理实现英文分词">
                使用有限状态机原理实现英文分词 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.png"
               alt="Kingname" />
          <p class="site-author-name" itemprop="name">Kingname</p>
          <p class="site-description motion-element" itemprop="description">爬虫工程师，极客学院讲师，Python, Scrapy, MongoDB, Redis, Pandas, Golang。</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">42</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              
                <span class="site-state-item-count">13</span>
                <span class="site-state-item-name">分类</span>
              
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">66</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/kingname" target="_blank" title="Github">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  Github
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="mailto:contact@kingname.info" target="_blank" title="Mail">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  Mail
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Friends
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://chuangzaoshi.com/" title="创造狮" target="_blank">创造狮</a>
                </li>
              
            </ul>
          </div>
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#功能介绍"><span class="nav-number">1.</span> <span class="nav-text">功能介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#实现原理"><span class="nav-number">2.</span> <span class="nav-text">实现原理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Workflowy"><span class="nav-number">2.1.</span> <span class="nav-text">Workflowy</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#登录Workflowy"><span class="nav-number">2.1.1.</span> <span class="nav-text">登录Workflowy</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#获取所有条目"><span class="nav-number">2.1.2.</span> <span class="nav-text">获取所有条目</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#提取任务与子任务"><span class="nav-number">2.1.3.</span> <span class="nav-text">提取任务与子任务</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Teambition"><span class="nav-number">3.</span> <span class="nav-text">Teambition</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#登录Teambition"><span class="nav-number">3.1.</span> <span class="nav-text">登录Teambition</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#设置Teambition应用"><span class="nav-number">3.1.1.</span> <span class="nav-text">设置Teambition应用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#使用Python获取access-token"><span class="nav-number">3.1.2.</span> <span class="nav-text">使用Python获取access_token</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Teambition的简单使用"><span class="nav-number">3.1.3.</span> <span class="nav-text">Teambition的简单使用</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#效果测试"><span class="nav-number">4.</span> <span class="nav-text">效果测试</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Kingname</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  





  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

  


</body>
</html>
